package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import android.widget.Toast;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.ConcreteParamEntity;
import com.sgb.kjwl.model.entity.STDetailEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.STDetailActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class STDetailModel extends BaseViewModel {
    public ObservableField<STDetailEntity> stDetailEntity = new ObservableField<>(new STDetailEntity());
    private STDetailActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 20;
    public int total;

    public STDetailModel(STDetailActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }


    public void getGoodsDetail(String id) {
        DialogHelper.showProgressDialog2(activity, null, "数据加载中...", 0, false, null, false);
        NewAPIManager.getInstance().getGoodsAPI().concreteDetail(id).enqueue(new ICallBack<BaseEntity<STDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<STDetailEntity> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                stDetailEntity.set(data.getData());
                activity.initData();
            }
        });
    }

    public String getNo() {
        return repertory.getNo();
    }

    //上下架删除商品
    public void upDowngetGoodsModel(String opt, List<String> goodsIds) {
        repertory.upDowngetGoods(opt, goodsIds)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.optSuccess(opt);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //商品详情改价
    public void modifyGoodsDetailPriceModel(String id, String price) {
        repertory.modifyGoodsDetailPrice(id, price)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.modifySuccess();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getPrice(ConcreteParamEntity data) {
        APIManager.getInstance().getMaterialAPI().getConcretePrice(data).enqueue(new Callback<BaseEntity<ConcreteParamEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<ConcreteParamEntity>> call, Response<BaseEntity<ConcreteParamEntity>> response) {
                if (response.body() == null || !response.body().getState().equals("ok")) return;
                activity.showPrice(response.body().getData().getTotalPrice());
            }

            @Override
            public void onFailure(Call<BaseEntity<ConcreteParamEntity>> call, Throwable t) {

            }
        });
    }
}
